package com.ml.mall.service.credit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.credit.*;
import com.ml.mall.bean.res.credit.CreditQueryInfoRes;
import com.ml.mall.bean.res.credit.CreditQueryRes;
import com.ml.mall.entity.credit.SysCredit;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.credit.SysCreditMapper;
import com.ml.mall.mapper.credit.SysCreditRecordMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.asyn.AuthMailServiceImplAsyn;
import com.ml.mall.service.credit.SysCreditRecordService;
import com.ml.mall.service.credit.SysCreditService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SysCreditServiceImpl extends ServiceImpl<SysCreditMapper, SysCredit> implements SysCreditService {

    @Autowired
    private SysCreditMapper sysCreditMapper;
    @Autowired
    private SysCreditRecordMapper sysCreditRecordMapper;
    @Lazy
    @Autowired
    private AuthMailServiceImplAsyn authMailServiceImplAsyn;

    @Override
    public ApiRestResult createCredit(CreditNewDto dto) {
        SysUser currentUser = JwtUtil.getCurrentUser();
        // 每个公司只有一条记录
        SysCredit dbSysCredit = sysCreditMapper.selectByCompanyId(dto.getCompanyId());
        if(dbSysCredit != null){
            throw new BizException(CommonEnum.ALREADY_EXISTS_COMPANY_CREDIT);
        }
        SysCredit sysCredit = new SysCredit();
        sysCredit.setCompanyId(dto.getCompanyId());
        sysCredit.setCreditBalance(dto.getCreditBalance());
        sysCredit.setUpdaterAcc(currentUser.getAcc());
        int isAdd = sysCreditMapper.insert(sysCredit);
        if(isAdd < 1){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return ApiRestResult.ok();
    }

    @Override
    public CreditQueryInfoRes queryOneCredit(CreditQueryOneDto dto) {
        LambdaQueryWrapper<SysCredit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getId() != null,SysCredit::getId,dto.getId())
               .eq(dto.getCompanyId() != null,SysCredit::getCompanyId,dto.getCompanyId());
        if(dto.getId() == null && dto.getCompanyId() == null){
            return null;
        }
        CreditQueryInfoRes creditQueryInfoRes = new CreditQueryInfoRes();
        SysCredit sysCredit = sysCreditMapper.selectOne(wrapper);
//        if(dto.getCompanyId() != null){
//            CreditRecordQueryDto queryDto = new CreditRecordQueryDto();
//            queryDto.setCompanyId( dto.getCompanyId());
//            BigDecimal initialBalance = sysCreditRecordMapper.sumCreditRecordMoney(queryDto);
//            //重新设置总额
//            sysCredit.setCreditBalance(initialBalance);
//        }
        if(sysCredit == null){
            return null;
        }
        BeanUtils.copyProperties(sysCredit,creditQueryInfoRes);
        return creditQueryInfoRes;
    }

    @Override
    public IPage<CreditQueryRes> queryPageCredit(CreditQueryDto dto) {
        Page<CreditQueryRes> page = new Page<>(dto.getPageIndex(), dto.getPageNum());
        List<CreditQueryRes> creditList = sysCreditMapper.selectPageCredit(dto);
        Long count = sysCreditMapper.selectCountCredit(dto);
        page.setTotal(count);
        page.setRecords(creditList);
        return page;
    }

    @Override
    public BigDecimal getBalanceByCompanyId(Long companyId) {
        SysCredit sysCredit = sysCreditMapper.selectByCompanyId(companyId);
        return sysCredit==null?BigDecimal.ZERO:sysCredit.getCreditBalance();
    }

    @Override
    @Transactional
    public void creditEdit(CreditEditDto dto) {
        SysCredit sysCredit = sysCreditMapper.selectById(dto.getId());
        SysCredit entity = new SysCredit();
        entity.setId(dto.getId());
        entity.setCreditBalance(dto.getCredit());
        sysCreditMapper.updateById(entity);
        authMailServiceImplAsyn.creditEditEmail(sysCredit,dto);
    }
}
